import gdata.gauth
import gdata.contacts.client
from django.http import HttpResponseRedirect

CONSUMER_KEY = 'take.8minut.es'
CONSUMER_SECRET = 'w1usAxob4zjpZUzsyMTwQio_'
SCOPES = ['https://www.google.com/m8/feeds/']

oauth_callback_url = 'http://8minutes.trezor.by/get_access_token/'


def get_request_token(request):
    client = gdata.contacts.client.ContactsClient()
    request_token = client.GetOAuthToken(
        SCOPES, oauth_callback_url, CONSUMER_KEY, consumer_secret=CONSUMER_SECRET)
    request.session['request_token'] = request_token
    return HttpResponseRedirect(request_token.generate_authorization_url())


def get_access_token(request):
    client = gdata.contacts.client.ContactsClient()
    saved_request_token = request.session.get('request_token')
    path = request.get_full_path()
    request_token = gdata.gauth.AuthorizeRequestToken(saved_request_token, path)
    access_token = client.GetAccessToken(request_token)
    client.auth_token = gdata.gauth.OAuthHmacToken(CONSUMER_KEY,
                                                   CONSUMER_SECRET,
                                                   access_token.token, access_token.token_secret,
                                                   gdata.gauth.ACCESS_TOKEN)
    contacts = client.GetContacts()
    return HttpResponseRedirect('/')